﻿/************************************************************************

 This file is part of "10 Second Tim".

 "10 Second Tim" is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 "10 Second Tim" is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with "10 Second Tim".  If not, see <http://www.gnu.org/licenses/>.

************************************************************************/

// All code (c)2013 Scott Bevin. all rights reserved

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace LDE.Input
{
	public class InputHandler
	{
		public void AddDevice( IInputDevice device )
		{
			if ( device == null )
				return;

			if ( !m_devices.Contains( device ) )
			{
				m_devices.Add( device );
			}
		}

		public void RemoveDevice( IInputDevice device )
		{
			m_devices.Remove( device );
		}

		public void ClearDevices( )
		{
			m_devices.Clear( );
		}

		public bool AnyDevicesConnected( )
		{
			foreach ( IInputDevice device in m_devices )
				if ( device.IsConnected( ) )
					return true;

			return false;
		}

		public IInputDevice FindDevice( int deviceTypeId, int deviceId )
		{
			foreach ( IInputDevice device in m_devices )
				if ( device.GetDeviceTypeId( ) == deviceTypeId && ( device.GetDeviceId( ) == deviceId || deviceId == -1 ) )
					return device;

			return null;
		}

		public bool HasPointingDevice( )
		{
			foreach ( IInputDevice device in m_devices )
			{
				if ( device.IsPointingDevice( ) )
					return true;
			}

			return false;
		}

		public InputButton IsPressed( InputTrigger trigger )
		{
			foreach ( IInputDevice device in m_devices )
			{
				InputButton button = ( device.IsPressed( trigger ) );

				if ( button != null )
					return button;
			}

			return null;
		}

		public InputButton IsDown( InputTrigger trigger )
		{
			foreach ( IInputDevice device in m_devices )
			{
				InputButton button = ( device.IsDown( trigger ) );

				if ( button != null )
					return button;
			}

			return null;
		}

		public InputButton IsHeld( InputTrigger trigger )
		{
			foreach ( IInputDevice device in m_devices )
			{
				InputButton button = ( device.IsHeld( trigger ) );

				if ( button != null )
					return button;
			}

			return null;
		}

		public InputButton IsReleased( InputTrigger trigger )
		{
			foreach ( IInputDevice device in m_devices )
			{
				InputButton button = ( device.IsReleased( trigger ) );

				if ( button != null )
					return button;
			}

			return null;
		}

		public InputButton IsTapped( InputTrigger trigger )
		{
			foreach ( IInputDevice device in m_devices )
			{
				InputButton button = ( device.IsTapped( trigger ) );

				if ( button != null )
					return button;
			}

			return null;
		}

		public Vector2 GetStickVals( )
		{
			Vector2 largest = Vector2.Zero;

			foreach ( IInputDevice device in m_devices )
			{
				Vector2 stickVal = device.GetStickVals( );

				if ( Math.Abs( stickVal.X ) > Math.Abs( largest.X ) )
					largest.X = stickVal.X;

				if ( Math.Abs( stickVal.Y ) > Math.Abs( largest.Y ) )
					largest.Y = stickVal.Y;
			}

			return largest;
		}

		public Point GetPointerPos( )
		{
			foreach ( IInputDevice device in m_devices )
			{
				if ( device.IsPointingDevice( ) )
					return device.GetPointerPos( );
			}

			return Point.Zero;
		}

		private List<IInputDevice> m_devices = new List<IInputDevice>( );
	}
}